/**
* @file DebugOutput.cpp
* @Description: DebugOutput 日志输出重定向 控制类主要用于日志的重定向输出（文件 or stdrr）
* @Date: 2020-02-26
* @LastEditTime: 2020-02-26
* @LastEditors: twosilly
* @Author: twosilly
* @E-Mail:twosilly@foxmail.com
*/

#include "DebugOutput.h"
#include <QDataStream>
namespace Logger {
//日志生成
void setDebugOutput(const QString &rawTargetFilePath_, const bool &argDateFlag_,const Flag flag,const logger_level level,bool more_)
{
    static QString rawTargetFilePath;
    static bool argDateFlag;
    static logger_level _level = level;
    static Flag _flag = flag;
    static bool more = more_;
    rawTargetFilePath = rawTargetFilePath_;
    argDateFlag = argDateFlag_;
    class HelperClass
    {
    private:
        static logger_level QtMsgType2logger_level(QtMsgType type){
            switch ( type )
            {
                case QtDebugMsg:
                     return logger_level::DEBUG;
                case QtInfoMsg:
                    return logger_level::INFO;
                case QtWarningMsg:
                    return logger_level::WARN;
                case QtCriticalMsg:
                    return logger_level::CRITICAL;
                case QtFatalMsg:
                    return logger_level::FATAL;
                default: { break; }
            }
            return logger_level::OFF;
        }
    public:

        static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message_)
        {
            QString message;          
            
            switch ( type )
            {
                case QtDebugMsg:
                {
                if(logger_level::DEBUG&_level){
                                           
                        message.append("Debug: ");          
                        
                        message.append(message_);
                    }
                    break;
                }
                case QtInfoMsg:
                {
                    if(logger_level::INFO&_level){
                           
                            message.append("Info: ");
                            message.append(message_);
                        }
                    break;
                }
                case QtWarningMsg:
                {
                    if(logger_level::WARN&_level){
                            
                        
                            message.append("Warning: ");
                            message.append(message_);
                        }
                    break;
                }
                case QtCriticalMsg:
                {
                    if(logger_level::CRITICAL&_level){
                           
                        
                            message.append("Critical: ");
                            message.append(message_);
                        }
                    break;
                }
                case QtFatalMsg:
                {
                    if(logger_level::FATAL&_level){                       
                        
                            message.append("Fatal: ");
                            message.append(message_);
                        }
                    break;
                }
                default: { break; }
            }

            QString currentTargetFilePath;

            if ( argDateFlag )
            {
                currentTargetFilePath = rawTargetFilePath.arg( ( ( argDateFlag ) ? ( QDateTime::currentDateTime().toString("yyyy_MM_dd") ) : ( "" ) ) );
            }
            else
            {
                currentTargetFilePath = rawTargetFilePath;
            }
            if ( !QFileInfo::exists( currentTargetFilePath ) )
            {
                QDir().mkpath( QFileInfo( currentTargetFilePath ).path() );
            }
            QString info;
            if(QtMsgType2logger_level(type)&_level)
            {
                info  += QString("[ file:%1").arg(context.file);
                info  += QString(" line:%1").arg(context.line);
                info  += QString(" function:%1").arg(context.function) + " ]\n"; 
                info  += QDateTime::currentDateTime().toString( "hh:mm:ss " );
                info  += QString("-->");
            }
            
            //!输出位置
            if(_flag&Flag::OUTPUT_STDERR&& !message.isEmpty())
            {
                if(more)
                    fprintf(stderr,"%s ",info.toLocal8Bit().constData());
                // fprintf(stderr,"%s \r\n",message.toLocal8Bit().constData());
                 fprintf(stderr,"%s\n",message.toLocal8Bit().constData());
            }
            if(_flag&Flag::OUTPUT_FILE && !message.isEmpty())
            {
                QFile file( currentTargetFilePath );
                file.open( QIODevice::WriteOnly | QIODevice::Append );    
                QTextStream textStream( &file );  
                if(more)
                     textStream <<info;
                textStream << message 
                           << "\r"<<endl;
                file.close();
            }

        }
    };

    qInstallMessageHandler( HelperClass::messageHandler );
}

} // namespace Logger



